package com.chen.controller;
import com.chen.entity.FlightEntity;
import com.chen.entity.UserEntity;
import com.chen.service.FlightService;
import com.chen.service.UserService;
import com.chen.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
public class FlightController {

    @Autowired
    private UserService userService;

    @Autowired
    private FlightService flightService;

    @RequestMapping("showFlight")
    public ModelAndView showFlight(
            @RequestParam(value = "flag", required = false)
            Integer flag,
            String phone, String departureAirport, String arriveAirport) {
        //1、控制层调用业务逻辑层获取数据
        ModelAndView modelAndView = new ModelAndView();
        List<FlightEntity> flightEntities = null;
        if ((departureAirport == null || departureAirport == "") &&
                (arriveAirport == null || arriveAirport == "") /*&&
                (flag == null)*/) {
            //flightEntities = flightService.searchByAll(departureAirport, arriveAirport);
            flightEntities = flightService.getByAll();
        } else {
            flightEntities = flightService.searchByAll(departureAirport, arriveAirport);
            //flightEntities = flightService.getByAll();
        }
        UserEntity userEntity = userService.getByPhoneUser(phone);
        if (userEntity.getId() == 1) {
            //2、转发到jsp中
            //管理员登陆
            modelAndView.addObject("flightEntities", flightEntities);
            modelAndView.addObject("userEntity", userEntity);
            modelAndView.setViewName("showRoot");
        } else {
            //2、转发到jsp中
            //普通用户登陆
            modelAndView.addObject("flightEntities", flightEntities);
            modelAndView.addObject("userEntity", userEntity);
            modelAndView.setViewName("showUser");
        }
        return modelAndView;
    }

    @PostMapping("insertFlight")
    public ModelAndView insertFlight(String phone, String flightId, String company, String departureAirport, String arriveAirport, String departureTime, String arriveTime, String model, String cost) {
        try {
            if (StringUtils.isEmpty(phone)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + phone + "为空值");
            }

            if (StringUtils.isEmpty(flightId)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + flightId + "为空值");
            }

            if (StringUtils.isEmpty(company)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + company + "为空值");
            }

            if (StringUtils.isEmpty(departureAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + departureAirport + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + arriveAirport + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + departureTime + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + arriveTime + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + model + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + model + "为空值");
            }


            // 将日期string类型转化成date
            Date departureTimeDate = DateUtils.stringToDate(departureTime);
            Date arriveTimeDate = DateUtils.stringToDate(arriveTime);
            Float costStr = Float.parseFloat(cost);

            //将数据置入实体中
            FlightEntity flightEntity = flightService.setEntity(null, flightId, company, departureAirport, arriveAirport, departureTimeDate, arriveTimeDate, model, costStr);

            int result = flightService.insertFlight(flightEntity);
            if (result <= 0) {
                // 修改失败的情况下
                return new ModelAndView("error", "errorMsg", "错误原因：插入失败为空值");

            }
            // 如果插入成功的情况下  则重定向到showFlight 查询航班信息
            return new ModelAndView("redirect:/showFlight", "phone", phone);
        } catch (Exception e) {
            return new ModelAndView("error", "errorMsg", "系统错误");

        }
    }


    @GetMapping("deleteFlight")
    public ModelAndView deleteFlight(String phone, String id) {
        //根据主键id删除信息
        if (id == null || id == "") {
            //跳转到错误页面，客户端传递有误
            return new ModelAndView("error", "errorMsg", "id的值为空");
        }
        try {
            Integer ids = Integer.parseInt(id);
            //int result = flightService.deleteFlightById(id);
            int result = flightService.updateDeleteById(ids);
            if (result > 0) {
                //如果删除成功，则跳转(转发)到 /showFlight 页面
                return new ModelAndView("redirect:/showFlight", "phone", phone);
            } else {
                //如果删除失败，则跳转(转发)到 error.jsp 页面
                return new ModelAndView("error", "errorMsg", "删除失败");
            }
        } catch (Exception e) {
            //该异常为类型转换错误
            e.printStackTrace();
            return new ModelAndView("error", "errorMsg", e.getMessage());
        }
    }

    @GetMapping("toUpdateFlight")
    public ModelAndView toUpdateFlight(String phone, String id) {
        if (StringUtils.isEmpty(phone)) {
            return new ModelAndView("error", "errorMsg", "错误原因：" + phone + "为空值");
        }
        if (StringUtils.isEmpty(id)) {
            return new ModelAndView("error", "errorMsg", "id的值为空");
        }
        try {
            //获取id
            Integer idStr = Integer.parseInt(id);
            FlightEntity flightEntity = flightService.getByIdFlight(idStr);
            if (flightEntity == null) {
                return new ModelAndView("error", "errorMsg", "该id不在数据库中");
            }

            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("f", flightEntity);
            modelAndView.addObject("phone", phone);
            modelAndView.setViewName("updateFlight");
            return modelAndView;
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error", "errorMsg", e.getMessage());
        }
    }


    @PostMapping("updateFlight")
    public ModelAndView updateFlight(String phone, String id, String flightId, String company, String departureAirport, String arriveAirport, String departureTime, String arriveTime, String model, String cost) {
        try {
            if (StringUtils.isEmpty(phone)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + phone + "为空值");
            }

            if (StringUtils.isEmpty(id)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + id + "为空值");
            }

            if (StringUtils.isEmpty(flightId)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + flightId + "为空值");
            }

            if (StringUtils.isEmpty(company)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + company + "为空值");
            }

            if (StringUtils.isEmpty(departureAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + departureAirport + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + arriveAirport + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + departureTime + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + arriveTime + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + model + "为空值");
            }

            if (StringUtils.isEmpty(arriveAirport)) {
                return new ModelAndView("error", "errorMsg", "错误原因：" + model + "为空值");
            }

            //验证id的值，验证id是否为空
            Integer idStr = Integer.parseInt(id);
            //将String转化为Data
            Date departureTimeDate = DateUtils.stringToDate(departureTime);
            Date arriveTimeDate = DateUtils.stringToDate(arriveTime);
            Float costF = Float.parseFloat(cost);

            //将修改的值置入实体中
            FlightEntity flightEntity = flightService.setEntity(idStr, flightId, company, departureAirport, arriveAirport, departureTimeDate, arriveTimeDate, model, costF);
            int result = flightService.updateFlight(flightEntity);
            if (result <= 0) {
                //如果修改失败，则跳转(转发)到 error.jsp 页面
                return new ModelAndView("error", "errorMsg", "修改失败");
            }
            //如果修改成功
            ModelAndView modelAndView = new ModelAndView();
            //Integer flag = 1;
            //modelAndView.addObject("flag", flag);
            modelAndView.addObject("phone", phone);
            modelAndView.setViewName("redirect:/showFlight");
            return modelAndView;
            //return new ModelAndView("redirect:/showFlight", "phone", phone);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("error", "errorMsg", e.getMessage());
        }
    }

    @GetMapping("/buyTicket")
    public ModelAndView buyTicket(String phone, String id) {
        if (StringUtils.isEmpty(phone)) {
            return new ModelAndView("error", "errorMsg", "错误原因：" + phone + "为空值");
        }
        if (StringUtils.isEmpty(id)) {
            return new ModelAndView("error", "errorMsg", "错误原因：" + id + "为空值");
        }
        Integer idStr = Integer.parseInt(id);
        FlightEntity flightEntity = flightService.getByIdFlight(idStr);
        UserEntity userEntity = userService.getByPhoneUser(phone);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("flightEntity", flightEntity);
        modelAndView.addObject("userEntity", userEntity);
        modelAndView.setViewName("buyTicket");
        return modelAndView;
    }
}


